<?php

/**
 *  QQ授权登录返回函数
 * 
 * @author        dwheng
 * @copyright     Copyright (c) 2015. All rights reserved.
 */
namespace frontend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\base\ErrorException;
use yii\helpers\Html;
use common\models\member\Member;
use common\models\notice\Notice;
use yii\web\UploadedFile;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\QCclass;
use frontend\models\FenhuiForm;
use common\models\Setting\Oauthqq;
use common\models\adminlog\AdminLog;

class QqCallBackAction extends Controller {

    public function actionIndex() {
        $qc = new QCclass();
        $access_token = $qc->qq_callback();
        $openid = $qc->get_openid();
        $qcL = new QCclass($access_token, $openid);
        $user_info = $qcL->get_user_info();
        //查看是否已绑定
        $bind = Oauthqq::model()->findByPk($openid);

        //数据
        $data = array(
            'type'         => 'qq',
            'access_token' => $access_token,
            'openid'       => $openid,
            'uid'          => $bind ? $bind->uid : 0,
            'username'     => $user_info['nickname'],
            'avatar'       => $user_info['figureurl_2']
        );

        //绑定注册
        $this->bind_register($bind, $data);
    }
	
	/**
	 * 绑定注册
     * 
	 * @param $bind 是否已经绑定
	 * @param $data 接收的第三方用户数据
	 */
	public function bind_register($bind, $data=array())
    {
		if(!$data['username']){
			echo "<script>alert('登录失败！');window.location.href='/auth/signup';</script>";
		}
		//初始密码
        $initPwd = substr(md5($data['username'].'_'.$data['type']), 0, 8);
		if(!$bind){
			
			//生成本地用户名
			$username = $data['username'].'_'.$data['type'];
            
			$model = new Oauthqq();
			$model->uid = 0; 
			$model->openid = $data['openid'];
			$model->access_token = $data['access_token'];

			//判断用户名是否已经存在
			$if_exist = Member::model()->find('username=:username', array(':username'=>$username));
			if(!$if_exist){
				//注册本地账号
				$adlog = new AdminLog();
				$user_model = new Member();
				$user_model->username = $username; //分配用户名
				$user_model->setPassword($password);
				$user_model->generateAuthKey();
				//$user_model->avatar = $data['avatar'];     //调用空间头像(100*100)
				$user_model->status = 10;
				$user_model->ip = $adlog->real_ip();  //注册ip
				if($user_model->save()){
					$model->uid = $uid = $user_model->uid;
					//保存第三方授权信息
					$model->save();
					
					//保存远程图片到本地
					/*$avatar_name = 'small_'.$user_model->uid.'_'.substr(md5(uniqid('file')), 0,11);
					$filepath = 'upload/avatar/'.date('Ym',time());
					$remote = Helper::downloadImage($data['avatar'], ROOT_PATH.'/'.$filepath, $avatar_name);
					if($remote){
						$local_avatar = $filepath.'/'.$remote['filename'];
						$bind_user = User::model()->findByPk($uid);
						$bind_user->avatar = $local_avatar;
						$bind_user->save();
					}*/
					
				}else{
					echo "<script>alert('登录失败！');window.location.href='/auth/signup';</script>";
				}	
			}else{
				$uid = $if_exist->id;
				$model->uid = $uid;
				//保存绑定qq用户
				$model->save();
			}			
						
		}else{
			//已经绑定
			$uid = $data['uid'];
		}
		$user = Member::model()->findByPk($uid);
		$username = $user->username;
		
		//自动登录
		if (Yii::$app->getUser()->login($user)) {
            return $this->redirect(['/member/info']);
        }else{
			echo "<script>alert('登录失败！');window.location.href='/auth/signup';</script>";
		}
	}	

}
